package com.example.wallpaperapp.ui.activity;

import androidx.annotation.Nullable;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.wallpaperapp.R;
import com.example.wallpaperapp.adapter.ClassifyRightMenuAdapter;
import com.example.wallpaperapp.adapter.ClassifyLeftMenuAdapter;
import com.example.wallpaperapp.base.BaseActivity;
import com.example.wallpaperapp.bean.ClassifyBean;
import com.facebook.drawee.backends.pipeline.Fresco;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ClassifyActivity extends BaseActivity {
    private List<String> menuList = new ArrayList<>();
    private List<ClassifyBean.DataBean> homeList = new ArrayList<>();
    private List<Integer> showTitle;
    private ListView lv_menu;
    private ListView lv_home;
    private ClassifyLeftMenuAdapter classifyLeftMenuAdapter;
    private ClassifyRightMenuAdapter classifyRightMenuAdapter;
    private int currentItem;
    private TextView tv_title;
    private ImageView back;
    private String json = "{\n" +
            "  \"code\": 0,\n" +
            "  \"data\": [\n" +
            "    {\n" +
            "      \"type\": \"category\",\n" +
            "      \"dataList\": [\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        }\n" +
            "      ],\n" +
            "      \"moreIcon\": \"https://wicdn.xiaohongchun.com/xhc-plat/1499690581646_JYRiS8rkdp.png\",\n" +
            "      \"moreText\": \"\",\n" +
            "      \"moduleStyle\": \"nothing\",\n" +
            "      \"moduleTitle\": \"左侧分类1\",\n" +
            "      \"moreLinkType\": \"storeTags\",\n" +
            "      \"interfaceLink\": \"\",\n" +
            "      \"moreLinkParam\": \"\",\n" +
            "      \"moreTextDisplay\": \"true\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"type\": \"category\",\n" +
            "      \"dataList\": [\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"id\": \"\",\n" +
            "          \"desc\": \"\",\n" +
            "          \"type\": \"category\",\n" +
            "          \"title\": \"虫子\",\n" +
            "          \"imgURL\": \"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1960756049,454975726&fm=26&gp=0.jpg\",\n" +
            "          \"linkType\": \"storeTags\",\n" +
            "          \"linkParam\": \"\",\n" +
            "          \"online_time\": \"\",\n" +
            "          \"offline_time\": \"\"\n" +
            "        }\n" +
            "      ],\n" +
            "      \"moreIcon\": \"https://wicdn.xiaohongchun.com/xhc-plat/1499690569496_jKyaBHe2Ze.png\",\n" +
            "      \"moreText\": \"\",\n" +
            "      \"moduleStyle\": \"nothing\",\n" +
            "      \"moduleTitle\": \"分类2\",\n" +
            "      \"moreLinkType\": \"topic\",\n" +
            "      \"interfaceLink\": \"\",\n" +
            "      \"moreLinkParam\": \"\",\n" +
            "      \"moreTextDisplay\": \"true\"\n" +
            "    }\n" +
            "  ]\n" +
            "}";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    @Override
    protected int initLayout() {
        return R.layout.activity_classify;
    }

    @SuppressLint("RestrictedApi")
    @Override
    protected void initView() {
        back=(ImageView)findViewById(R.id.ad_back);
        lv_menu = (ListView) findViewById(R.id.lv_menu);
        tv_title = (TextView) findViewById(R.id.tv_titile);
        lv_home = (ListView) findViewById(R.id.lv_home);
        classifyLeftMenuAdapter = new ClassifyLeftMenuAdapter(this, menuList);
        lv_menu.setAdapter(classifyLeftMenuAdapter);
        classifyRightMenuAdapter = new ClassifyRightMenuAdapter(this, homeList);
        lv_home.setAdapter(classifyRightMenuAdapter);
        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                classifyLeftMenuAdapter.setSelectItem(position);
                classifyLeftMenuAdapter.notifyDataSetInvalidated();
                tv_title.setText(menuList.get(position));
                lv_home.setSelection(showTitle.get(position));
            }
        });
        lv_home.setOnScrollListener(new AbsListView.OnScrollListener() {
            private int scrollState;
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                this.scrollState = scrollState;
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    return;
                }
                int current = showTitle.indexOf(firstVisibleItem);
//				lv_home.setSelection(current);
                if (currentItem != current && current >= 0) {
                    currentItem = current;
                    tv_title.setText(menuList.get(currentItem));
                    classifyLeftMenuAdapter.setSelectItem(currentItem);
                    classifyLeftMenuAdapter.notifyDataSetInvalidated();
                }
            }
        });
    }
    @Override
    protected void initData() {
        //String json = getJson(this, "category.json");
        ClassifyBean classifyBean = JSONObject.parseObject(json, ClassifyBean.class);
        showTitle = new ArrayList<>();
        for (int i = 0; i < classifyBean.getData().size(); i++) {
            ClassifyBean.DataBean dataBean = classifyBean.getData().get(i);
            menuList.add(dataBean.getModuleTitle());
            showTitle.add(i);
            homeList.add(dataBean);
        }
        tv_title.setText(classifyBean.getData().get(0).getModuleTitle());

        classifyLeftMenuAdapter.notifyDataSetChanged();
        classifyRightMenuAdapter.notifyDataSetChanged();
    }
    /**
     * 得到json文件中的内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}